package com.caiyigo.baihua.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;

public class BaseController {
	private final static String Redirect = "redirect:";
	private final static String Forward = "forward:";

	/**
	 * 
	 * @param attr
	 *            RedirectAttributes
	 * @param messages
	 *            参数
	 * @param url
	 *            Action地址
	 * @return
	 */
	public String RedirectAction(RedirectAttributes attr,
			Map<String, String> messages, String url) {
		if (null != messages && messages.size() > 0) {
			for (String key : messages.keySet()) {
				attr.addFlashAttribute(key, messages.get(key));
			}
		}
		return RedirectAction(url);
	}

	public String RedirectAction(String url) {
		return Redirect + url;
	}

	public String ForwardToPage(String page) {
		return Forward + page;
	}

	public void renderHtml(
			HttpServletResponse response, String text) {
		PrintWriter writer = null;
		try {
			response.setHeader("Pragma", "no-cache"); // HTTP/1.0 caches might
														// not implement
														// Cache-Control and
														// might only implement
														// Pragma: no-cache
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);

			response.setContentType("text/html;charset=UTF-8");
			writer = response.getWriter();
			writer.write(text);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null)
				writer.close();
		}

	}

}
